package com.hm.quartz.task;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hm.common.core.domain.entity.SysDept;
import com.hm.common.core.domain.entity.SysUser;
import com.hm.manage.domain.Hardserver;
import com.hm.manage.domain.Xjjl;
import com.hm.manage.domain.Xjjlsub;
import com.hm.manage.dto.FileSizeInfo;
import com.hm.manage.mapper.HardserverMapper;
import com.hm.manage.mapper.XjjlMapper;
import com.hm.manage.service.IXjjlService;
import com.hm.manage.service.impl.HardserverServiceImpl;
import com.hm.manage.utils.FileSizeParser;
import com.hm.manage.utils.SmsService;
import com.hm.system.mapper.SysDeptMapper;
import com.hm.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component("checkTask")
public class CheckTask {

    @Autowired
    private SmsService smsService;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private HardserverServiceImpl hardserverService;

    @Autowired
    private HardserverMapper hardserverMapper;

    @Autowired
    private XjjlMapper xjjlMapper;

    @Autowired
    private IXjjlService xjjlService;


    public void checkTask1(String deptName,String limitccon)  {
        Double limitcon = Double.valueOf(limitccon);
        // 获取手机号码 要求是目录在海康下，非admin，手机号码不为空
        SysDept sysDept = new SysDept();
        sysDept.setDeptName(deptName);
        List<SysDept> sysDepts = sysDeptMapper.selectDeptList(sysDept);
        List<SysUser> sysUsers = new ArrayList<>();
        // 非空判断
        if (!sysDepts.isEmpty()) {
            for (SysDept sysDept1 : sysDepts) {
                // 获取部门下的所有人员的手机号码
                SysUser sysUser = new SysUser();
                sysUser.setDeptId(sysDept1.getDeptId());
                sysUsers = sysUserMapper.selectUserList(sysUser);
            }
        }
        // 判空
        if (sysUsers.isEmpty()) {
            log.info("---------{}","海康部门下没有获取到人员信息");
        } else {
            //开始巡检
            // 先获取服务器列表
            List<Hardserver> hardserverList = hardserverService.selectHardserverList(null);

            // 插入巡检记录的主表
            // 创建巡检记录对象
            Xjjl xjjl = new Xjjl();
            xjjl.setCreateTime(new Date());
            xjjl.setXjsj(new Date());
            xjjl.setXjlx("云平台服务器");
            xjjl.setXjly("自动");
            xjjl.setXjjg("1");//默认给正常，如果下面有不行的就给异常
            List<Xjjlsub> xjjlsubList = new ArrayList<>();
                // 巡检干系人
                String xjgxr="";

            // 遍历集合，巡检
            String content = "";
            for (Hardserver hardserver : hardserverList) {
                String serverInfo = hardserverService.getServerInfo(hardserver.getId());
                // 创建巡检子表
                Xjjlsub xjjlsub = new Xjjlsub();
                xjjlsub.setCreateTime(new Date());
                xjjlsub.setDeviceid(hardserver.getId());
                xjjlsub.setDeviceip(hardserver.getServerIp());
                    // 分析结果
                    FileSizeInfo fileSizeInfo = FileSizeParser.parseLine(serverInfo);
                xjjlsub.setXjnr(fileSizeInfo.getPath());
                xjjlsub.setXjdw(fileSizeInfo.getUnit());
                xjjlsub.setXjsize(fileSizeInfo.getSizeValue());
                xjjlsub.setYjrl(limitcon+"G");
                    // 解析结果
                if (FileSizeParser.getResult(fileSizeInfo,limitcon).equals("异常")) {
                    xjjl.setXjjg("2");//异常就给2
                }
                xjjlsub.setXjzt(FileSizeParser.getResult(fileSizeInfo,limitcon));
                xjjlsubList.add(xjjlsub);
                // 拼接返回的语句。
                content =content + hardserver.getServerIp()+"占用空间"+serverInfo+";\n";
            }
            xjjl.setXjnr(content);
            xjjl.setXjjlsubList(xjjlsubList);

            // 遍历人员发送短信
            // 开始遍历 逻辑：手机号码不为空，且不为管理员（userId!=1）
            for (SysUser sysUser : sysUsers) {
                if (sysUser.getUserId() == 1 || sysUser.getPhonenumber().isEmpty() || Objects.equals(sysUser.getDelFlag(), "2") || sysUser.getStatus().equals("1")) {
                    log.info("----------------{}---{}",sysUser.getUserName(),"不需要发短信：为管理员或手机号为空");
                } else {
                    log.info("----------------{}---{}",sysUser.getUserName(),"开始发短信");
                    String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +xjjl.getXjlx()+"巡检已完成，请及时关注巡检内容！巡检结果为：";
                    String messageres = "";
                    if (xjjl.getXjjg().equals("1")) {
                        // 正常
                        messageres=smsService.sendSms(sysUser.getPhonenumber(), s+"正常。请关注巡检细节。");
                    } else if (xjjl.getXjjg().equals("2")) {
                        messageres=smsService.sendSms(sysUser.getPhonenumber(), s+"异常。请关注巡检细节。");
                    }

                    if (messageres.equals("Success")) {
                        xjgxr = xjgxr + sysUser.getNickName()+"("+sysUser.getPhonenumber()+");";
                        log.info("---发送成功-------{}----{}---",sysUser.getUserName(),content);
                    }
                }
            }
            xjjl.setXjgxr(xjgxr);

            // 保存巡检记录
                // 检查长度
                log.debug("---有问题！！！！-----长度是：{}-",xjjl.getXjjlsubList().size());
            xjjlService.insertXjjl(xjjl);

        }

    }
}
